package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import entity.tDevice;

import service.tDeviceService;

public class UpdatetDeviceAction {
	
	private String deviceId;
	//private String deviceNo;
	//private String deviceName;
	private Timestamp addTime;
	private String deviceArea;
	private String place;
	private String deviceDescribe;
	private String mac;
	
	private int ph;
	private int orp;
	private int temperature;
	private int ec;
	private int openEC;
	private int closeEC;
	
	private tDeviceService tdeviceService;
	private String[] list;
	public String update() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		
		System.out.println("--------------经过UpdatetDeviceAction-------------");
		
		List<tDevice> lists = new ArrayList<tDevice>();
		
		
		
		System.out.println("Mac:" + mac);
		
		System.out.println("deviceId:" + deviceId);
		
		lists = tdeviceService.searchById(deviceId);
		
		tDevice tdevice = new tDevice();
		
		String deviceNo = lists.get(0).getDeviceNo();
		String deviceName = lists.get(0).getDeviceName();
		
		if (deviceNo.equals("")){
			deviceNo = "无序列号";
		}
		if (deviceName.equals("")){
			deviceName = "无名称";
		}
		System.out.println(deviceNo+"     "+deviceName);
		
		String industry = lists.get(0).getIndustry();
		String sketchmqp = lists.get(0).getSketchMap();
		String videourl = lists.get(0).getVideoUrl();
		
		if (industry.equals("")){
			industry = "无行业";
		}
		if (sketchmqp.equals("")){
			sketchmqp = "无图";
		}
		
		System.out.println("deviceNo:"+deviceNo+",deviceName:"+deviceName+",addTime:"+addTime+",deviceArea:"+deviceArea+",place:"+place+",deviceDescribe:"+deviceDescribe);
		
		tdevice.setDeviceId(deviceId);
		tdevice.setDeviceNo(deviceNo);
		tdevice.setDeviceName(deviceName);
		tdevice.setAddTime(addTime);
		tdevice.setDeviceArea(deviceArea);
		tdevice.setPlace(place);
		tdevice.setDeviceDescribe(deviceDescribe);
		tdevice.setDistributeTime(addTime);
		tdevice.setMac(mac);
		
		System.out.println(list);
		System.out.println(list.length);
		tdevice.setPh(0);
		tdevice.setOrp(0);
		tdevice.setTemperature(0);
		tdevice.setEc(0);
		tdevice.setOpenEC(0);
		tdevice.setCloseEC(0);
		tdevice.setEsil(0);
		tdevice.setEc1(0);
		tdevice.setEc2(0);
		tdevice.setEc3(0);
		tdevice.setEc4(0);
		tdevice.setEc5(0);
		tdevice.setEc6(0);
		tdevice.setEc7(0);
		tdevice.setEc8(0);
		tdevice.setEc9(0);
		tdevice.setEc10(0);
		tdevice.setEc11(0);
		tdevice.setEc12(0);
		tdevice.setEc13(0);
		for (int i = 0; i < list.length; i++) {
			String shuiguoname=list[i];
			if (shuiguoname.equals("ph")){
				tdevice.setPh(1);
			}
			
			if (shuiguoname.equals("orp")){
				tdevice.setOrp(1);
			}
			
			if (shuiguoname.equals("temperature")){
				tdevice.setTemperature(1);
			}
			
			if (shuiguoname.equals("ec")){
				tdevice.setEc(1);
			}
			
			if (shuiguoname.equals("openEC")){
				tdevice.setOpenEC(1);
			}
			
			if (shuiguoname.equals("closeEC")){
				tdevice.setCloseEC(1);
			}
			if (shuiguoname.equals("esil")){
				tdevice.setEsil(1);
			}
			if (shuiguoname.equals("ec1")){
				tdevice.setEc1(1);
			}
			if (shuiguoname.equals("ec2")){
				tdevice.setEc2(1);
			}
			if (shuiguoname.equals("ec3")){
				tdevice.setEc3(1);
			}
			if (shuiguoname.equals("ec4")){
				tdevice.setEc4(1);
			}
			if (shuiguoname.equals("ec5")){
				tdevice.setEc5(1);
			}
			if (shuiguoname.equals("ec6")){
				tdevice.setEc6(1);
			}
			if (shuiguoname.equals("ec7")){
				tdevice.setEc7(1);
			}
			if (shuiguoname.equals("ec8")){
				tdevice.setEc8(1);
			}
			if (shuiguoname.equals("ec9")){
				tdevice.setEc9(1);
			}
			if (shuiguoname.equals("ec10")){
				tdevice.setEc10(1);
			}
			if (shuiguoname.equals("ec11")){
				tdevice.setEc11(1);
			}
			if (shuiguoname.equals("ec12")){
				tdevice.setEc12(1);
			}
			if (shuiguoname.equals("ec13")){
				tdevice.setEc13(1);
			}
			System.out.println(shuiguoname);
		}
		tdevice.setIndustry(industry);
		tdevice.setSketchMap(sketchmqp);
		tdevice.setVideoUrl(videourl);
		
		System.out.println(tdevice);
		tdeviceService.update(tdevice);
		
		return "success";
	}
	

	public String getDeviceId() {
		return deviceId;
	}


	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}


//	public String getDeviceNo() {
//		return deviceNo;
//	}
//
//
//	public void setDeviceNo(String deviceNo) {
//		this.deviceNo = deviceNo;
//	}
//
//
//	public String getDeviceName() {
//		return deviceName;
//	}
//
//
//	public void setDeviceName(String deviceName) {
//		this.deviceName = deviceName;
//	}


	public Timestamp getAddTime() {
		return addTime;
	}


	public void setAddTime(Timestamp addTime) {
		this.addTime = addTime;
	}


	public String getDeviceArea() {
		return deviceArea;
	}


	public void setDeviceArea(String deviceArea) {
		this.deviceArea = deviceArea;
	}


	public String getPlace() {
		return place;
	}


	public void setPlace(String place) {
		this.place = place;
	}


	public String getDeviceDescribe() {
		return deviceDescribe;
	}


	public void setDeviceDescribe(String deviceDescribe) {
		this.deviceDescribe = deviceDescribe;
	}


	public String getMac() {
		return mac;
	}


	public void setMac(String mac) {
		this.mac = mac;
	}


	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}


	public int getPh() {
		return ph;
	}


	public void setPh(int ph) {
		this.ph = ph;
	}


	public int getOrp() {
		return orp;
	}


	public void setOrp(int orp) {
		this.orp = orp;
	}


	public int getTemperature() {
		return temperature;
	}


	public void setTemperature(int temperature) {
		this.temperature = temperature;
	}


	public int getEc() {
		return ec;
	}


	public void setEc(int ec) {
		this.ec = ec;
	}


	public int getOpenEC() {
		return openEC;
	}


	public void setOpenEC(int openEC) {
		this.openEC = openEC;
	}


	public int getCloseEC() {
		return closeEC;
	}


	public void setCloseEC(int closeEC) {
		this.closeEC = closeEC;
	}


	public String[] getList() {
		return list;
	}


	public void setList(String[] list) {
		this.list = list;
	}
	
	

}
